package net.vkits.controller;

import net.vkits.admin.application.SyslogApplicationService;
import net.vkits.admin.domain.model.Syslog;
import net.vkits.common.Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 16/4/19.
 */
@Controller
@RequestMapping("syslogs")
public class SyslogController {
    private static final Logger log = LoggerFactory.getLogger(SyslogController.class);

    @Resource
    SyslogApplicationService syslogApplicationService;

    @RequiresPermissions("system:sys_log:list")
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView syslogs() {
        String username = Utils.getUsername(SecurityUtils.getSubject()).orElse("Null");
        ModelMap modelMap = new ModelMap();
        modelMap.put("treeview", "system");
        modelMap.put("sidebar", "syslogs");
        modelMap.put("currentUser",username);
        return new ModelAndView("/syslogs", modelMap);
    }

    @RequiresPermissions("system:sys_log:list")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Page<Syslog> getSyslogs(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)Pageable pageable) {
        Page<Syslog> syslogs = syslogApplicationService.getSyslogs(pageable);
        log.info("size is: {}", syslogs.getTotalElements());
        return syslogs;
    }
}
